// userPageC/pages/userPersonal/withdrawal/withdrawal.js
const app = getApp()
const WXAPI = require("../../../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: ' ', // 导航栏 中间的标题
      textColor: '#000', // 标题颜色
      bgColor: '#fff', // 导航栏背景颜色
      isloading: false,
      isColor: true,
    },
    money: '',
    isPassshow: false,
    isFocus: true, //是否自动聚焦
    Length: 6, //密码长度
    entryList: "",
    active: 0,
    withdrawMoney: '',
    nickName: app.globalData.nickName,
    moneyLs:[
      '100','200'
    ],
    changeIndex: 99,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDetail()
   
  },
  choice(e){
    let index = e.currentTarget.dataset.index
    console.log(index)
    if(index==0){
      this.data.money = 100
    }else{
      this.data.money = 200
    }
    
    this.setData({
      changeIndex: index
    })
  },
  getDetail(){
    var data = {}
    WXAPI.withdrawBalance(data,(res)=>{
      console.log('可提现余额',res)
      this.setData({
        withdrawMoney: res.result
      })
    })
  },
  //是否设置过支付密码
  checkPayPsw(){
    WXAPI.checkPayPsw('',(res)=>{
      console.log('是否设置过支付密码',res)
      if (res.code == 200) {
        this.setData({
          isPassshow: true,
        })
      }else{
        wx.showModal({
          title: '',
          content: '您还未设置安全码，为了更好的保障您的权益，请先前往设置安全码',
          cancelText: '放弃',
          cancelColor:'#B1B1B1',
          confirmText:'去设置',
          confirmColor:'#FF5800',
          success (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/userPageC/pages/userPersonal/changePayPass/changePayPass',
              })
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
             
            }
          }
        })
      }
    })
  },
  //获取输入金额
  // hangdleAmount(e){
  //   var money ;
  //   if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) { //正则验证，提现金额小数点后不能大于两位数字
  //     money = e.detail.value;

  //   } else {
  //     money = e.detail.value.substring(0, e.detail.value.length - 1);
  //     console.log('money',money)
  //   }
  //   this.setData({
  //     money: money,
  //   })
  // },
  handleWithdr(){
    // this.checkPayPsw()
    var money = this.data.money
    var withdrawMoney = this.data.withdrawMoney
    if(!money){
      wx.showToast({
        title: '请选择提现金额',
        icon: 'none'
      })
      return
    }else{
      if(money > withdrawMoney || withdrawMoney == 0){
        wx.showToast({
          title: '提现金额不足',
          icon: 'none'
        })
        return
      }else if(money == 0){
        wx.showToast({
          title: '输入金额要大于零元',
          icon: 'none'
        })
        return
      }
      this.checkPayPsw()
    }
    
  },
  closePay(){ //关闭密码弹窗
    let _this = this
    wx.showModal({
      title: '',
      content: '是否放弃本次提现',
      cancelText: '放弃',
      cancelColor:'#B1B1B1',
      confirmText:'继续提现',
      confirmColor:'#FF5800',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
          _this.setData({
            isPassshow: false,
            entryList: ''
          })
        }
      }
    })
  },
  inputFocus() { //点击聚焦
    this.setData({
        isFocus: true
    })
  },
  inputValue(e) { //监听密码输入
    var value = e.detail.value;
    var list= e.detail.value.split('')
    this.setData({
        entryList: value,
        active: list.length
    })
    console.log(list.length)
    console.log(value)
    console.log(this.data.entryList)
    if(list.length==6){
      console.log('++++++')
      var data = {
        payPassword: value,
        money: this.data.money
      }
      WXAPI.getWithdraw(data,(res)=>{
        if(res.code == 200){
          let result = JSON.stringify(res.result)
          this.setData({
            isPassshow: false,
          })
          wx.showToast({
            title: '提现成功',
          })
          setTimeout(() => {
            wx.redirectTo({
              url: '/userPageC/pages/userPersonal/withdrawalScuess/withdrawalScuess?result=' + result,
            })
          }, 1500);
        }else{
          console.log(list.length)
          this.setData({
            isPassshow: false,
            entryList: '',
          })
          setTimeout(() => {
            this.setData({
              isPassshow: true,
              entryList: '',
            })
          }, 500);
        }
        
      })
    }else{
      
    }
  },
  //忘记密码
  forget(){
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/changePayPass/changePayPass',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})